import { _decorator, Component, Label, NodeEventType, Node, Slider, UIRenderer, Color, Tween, Vec3, profiler } from 'cc';
const { ccclass, property } = _decorator;
@ccclass('HelloWorld')
export class HelloWorld extends Component {
    private _musicSwitcherOn: boolean = false;
    private get musicSwitcherOn(): boolean { return this._musicSwitcherOn; }
    private set musicSwitcherOn(val) {
        this._musicSwitcherOn = gi.musicOn = val;
        this.setActive(this.musicPlay0Node, val);
        this.setActive(this.musicPlay1Node, val);
        this.setActive(this.musicPauseNode, val);
        this.setActive(this.musicStopNode, val);
        new Tween(this.musicSwitcherNode)
            .to(0.1, { position: new Vec3(val ? 150 : 350, this.musicSwitcherNode.position.y) })
            .start();
    }
    private _soundSwitcherOn: boolean = false;
    private get soundSwitcherOn(): boolean { return this._soundSwitcherOn; }
    private set soundSwitcherOn(val) {
        this._soundSwitcherOn = gi.soundOn = val;
        this.setActive(this.soundPlayNode, val);
        new Tween(this.soundSwitcherNode)
            .to(0.1, { position: new Vec3(val ? 150 : 350, this.soundSwitcherNode.position.y) })
            .start();
    }
    private musicPlay0Node: Node = null;
    private musicPlay1Node: Node = null;
    private musicPauseNode: Node = null;
    private musicStopNode: Node = null;
    private musicSwitcherNode: Node = null;
    private musicSlider: Slider = null;
    private soundPlayNode: Node = null;
    private soundSwitcherNode: Node = null;
    private soundSlider: Slider = null;

    protected onLoad(): void {
        profiler.hideStats();
        this.musicPlay0Node = this.node.getChildByName('MusicPlay0');
        this.musicPlay1Node = this.node.getChildByName('MusicPlay1');
        this.musicPauseNode = this.node.getChildByName('MusicPause');
        this.musicStopNode = this.node.getChildByName('MusicStop');
        this.musicSwitcherNode = this.node.getChildByPath('MusicOn/Switcher');
        this.musicSlider = this.node.getChildByPath('MusicVolume/Slider').getComponent(Slider);
        this.soundPlayNode = this.node.getChildByName('SoundPlay');
        this.soundSwitcherNode = this.node.getChildByPath('SoundOn/Switcher');
        this.soundSlider = this.node.getChildByPath('SoundVolume/Slider').getComponent(Slider);

        this.musicPlay0Node.on(NodeEventType.TOUCH_START, () => {
            if (!this.musicPlay0Node['isActive']) return;
            gi.musicPlay('Res/Music0');
            this.setActive(this.musicPauseNode, true);
        }, this);
        this.musicPlay1Node.on(NodeEventType.TOUCH_START, () => {
            if (!this.musicPlay1Node['isActive']) return;
            gi.musicPlay('Res/Music1');
            this.setActive(this.musicPauseNode, true);
        }, this);
        this.musicPauseNode.on(NodeEventType.TOUCH_START, () => {
            if (!this.musicPauseNode['isActive']) return;
            let lb = this.musicPauseNode.getChildByName('Label').getComponent(Label);
            if (lb.string === '暂停音乐') {
                gi.musicPause();
                lb.string = '恢复音乐';
            } else {
                gi.musicResume();
                lb.string = '暂停音乐';
            }
        }, this);
        this.musicStopNode.on(NodeEventType.TOUCH_START, () => {
            if (!this.musicStopNode['isActive']) return;
            gi.musicStop();
            this.setActive(this.musicPauseNode, false);
        }, this);
        this.musicSwitcherNode.setPosition(gi.musicOn ? 150 : 350, this.musicSwitcherNode.position.y);
        this.musicSwitcherNode.on(NodeEventType.TOUCH_START, () => {
            this.musicSwitcherOn = !this.musicSwitcherOn;
        }, this);
        this.musicSlider.progress = gi.musicVolume;
        this.musicSlider.node.on('slide', () => {
            gi.musicVolume = this.musicSlider.progress;
        }, this);
        this.soundPlayNode.on(NodeEventType.TOUCH_START, () => {
            if (!this.soundPlayNode['isActive']) return;
            gi.soundPlay(`Res/Sound${Math.random() * 3 | 0}`);
        }, this);
        this.soundSwitcherNode.setPosition(gi.soundOn ? 150 : 350, this.soundSwitcherNode.position.y);
        this.soundSwitcherNode.on(NodeEventType.TOUCH_START, () => {
            this.soundSwitcherOn = !this.soundSwitcherOn;
        }, this);
        this.soundSlider.progress = gi.soundVolume;
        this.soundSlider.node.on('slide', () => {
            gi.soundVolume = this.soundSlider.progress;
        }, this);
        this.musicSwitcherOn = gi.musicOn;
        this.soundSwitcherOn = gi.soundOn;
    }
    private setActive(node: Node, isActive: boolean): void {
        node['isActive'] = isActive;
        let color = isActive ? new Color(255, 255, 255) : new Color(50, 50, 50);
        for (let urs = node.getComponentsInChildren(UIRenderer), i = urs.length - 1; i >= 0; urs[i--].color = color);
    }
}